//
//  HomeVIewController.swift
//  Automobile
//
//  Created by erkebulan elzhan on 7/25/20.
//  Copyright © 2020 erkebulan elzhan. All rights reserved.
//

import UIKit
import SnapKit

class HomeViewConroller: UIViewController {
    
    // MARK: - Properties && Outetls
    private lazy var tableView: UITableView = {
        let table = UITableView()
        table.backgroundColor = .white
        table.separatorStyle = .none
        table.delegate = self
        table.dataSource = self
        table.register(NavCell.self, forCellReuseIdentifier: "navCell")
        return table
    }()
    
    private var homeModel = HomeViewModel()
    private var headerView : HomeHeaderView!
    
    // MARK: - Application Life-Cycle
    
    override func viewDidLoad() {
        super.viewDidLoad()
        headerView = HomeHeaderView(user: homeModel.user)
        self.setUpViews()
        self.setUpNav()
    }

    
    // MARK: - UI Custom Styling
    
    private func setUpViews () {
        self.view.addSubview(headerView)
        self.view.addSubview(tableView)
        /// header view
        headerView.snp.makeConstraints { (make) in
            make.top.equalTo(view.safeAreaLayoutGuide).offset(16)
            make.height.equalTo(76)
            make.leading.trailing.equalToSuperview()
        }
        /// table view
        tableView.snp.makeConstraints { (make) in
            make.top.equalTo(headerView.snp.bottom).offset(12)
            make.leading.trailing.equalToSuperview().inset(16)
            make.bottom.equalTo(view.safeAreaLayoutGuide)
        }
    }
    private func setUpNav () {
        self.view.backgroundColor = .white
        self.tabBarController?.tabBar.isHidden = false
    }
}

extension HomeViewConroller: UITableViewDelegate, UITableViewDataSource {
    
    // MARK: - Section Of UITable View
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return homeModel.navItems.count
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let headerLbl = UILabel()
        headerLbl.frame = CGRect(x: 0, y: 0, width: tableView.frame.width, height: 40)
        headerLbl.textColor = .darkBlueColor
        headerLbl.font = UIFont.systemFont(ofSize: 17, weight: .medium)
        headerLbl.text = homeModel.navItems[section].sectionName
        headerLbl.textAlignment = .left
        
        return headerLbl
    }
    
    // MARK: - Rows
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return homeModel.navItems[section].rows.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "navCell") as! NavCell
        cell.navData = homeModel.navItems[indexPath.section].rows[indexPath.row]
        cell.selectionStyle = .none
        return cell
    }
    
    // MARK: - Properties
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 40.0
    }
    
    // MARK: - Action
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = homeModel.navItems[indexPath.section].rows[indexPath.row].destination {
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
